package com.xio.cloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.xio.cloud.service.PaymentFeignService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author: xio
 * @Description: 消费端控制层
 * @Date: 2020/9/30 15:29
 */
@RestController
@RequestMapping("/order")
@DefaultProperties(defaultFallback = "paymentGlobalFallabckMethod")
public class OrderController {

    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping("/payment/hystrix/ok/{id}")
    String paymentInfo_OK(@PathVariable("id") Integer id){
        return paymentFeignService.paymentInfo_OK(id);
    }

    @GetMapping("/payment/hystrix/timeout/{id}")
    // 特别指定异常回调方法
    /*@HystrixCommand(fallbackMethod = "paymentTimeOutFallbackMethod", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })*/
    // 没指定则用全局方法
    @HystrixCommand
    String paymentInfo_TimeOut(@PathVariable("id") Integer id){
        return paymentFeignService.paymentInfo_TimeOut(id);
    }

    public String paymentTimeOutFallbackMethod(@PathVariable("id") Integer id) {
        return "我是消费者80,对方支付系统繁忙请10秒种后再试或者自己运行出错请检查自己";
    }

    public String paymentGlobalFallabckMethod(){
        return "Global 全局异常提示：对方支付系统繁忙请10秒种后再试或者自己运行出错请检查自己";
    }

    @GetMapping("/payment/hystrix/circuit/{id}")
    String paymentCircuitBreaker(@PathVariable("id") Integer id){
        return paymentFeignService.paymentCircuitBreaker(id);
    }

}
